﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DwgPreviewLib;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using HYCodeBase.StringUtility;
using HYRevitCode.FormUtility;
using Teigha.Core;
using Teigha.TD;
using Transfer.Utils;

namespace HYPretreatmentModeling
{
	public partial class DWG2RevitLevelConfigForm : System.Windows.Forms.Form
	{
		public static bool b
		{
			set
			{
				DWG2RevitLevelConfigForm.m_b = value;
			}
		}

		public static bool bOK
		{
			get
			{
				return DWG2RevitLevelConfigForm.m_bOK;
			}
		}

		public DWG2RevitLevelConfigForm(ExternalCommandData cmdData, string OdDbDataBasePath)
		{
			this.InitializeComponent();
			this.m_ctrlManager = new ControlInfoManager(this, new List<KeyValuePair<System.Windows.Forms.Control, bool>>(), null, null);
			this.lvwDrawingList.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
			this.m_nDrawingListViewColumnWidthMin = this.lvwDrawingList.Columns[1].Width;
			this.m_viewAllDrawing.MouseClick += this.viewAllDrawing_MouseClick;
			this.tbxEditDrawingName.Parent = this.lvwDrawingList;
			this.m_app = cmdData.Application;
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_OdDbDataBasePath = OdDbDataBasePath;
			DWG2RevitLevelConfigForm.m_bOK = true;
			if (!DWG2RevitLevelConfigForm.m_b)
			{
				using (DWGDataManager dwgdataManager = new DWGDataManager(cmdData))
				{
					List<LoopInfo> list = new List<LoopInfo>();
					TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks);
					dwgdataManager.GetPolygonInnerValidObjs(this.m_OdDbDataBasePath, ref list);
					if (list.IsEmpty<LoopInfo>())
					{
						YJKMessageBox.Show("未检索到图框。");
					}
					else
					{
						TimeSpan ts = new TimeSpan(DateTime.Now.Ticks);
						timeSpan.Subtract(ts).Duration();
						this.m_lstViewDWGInfo = dwgdataManager.ExportBlockFrameDatabase2Files(list, OdDbDataBasePath, out this.m_strAimSaveFilePath);
						if (this.m_lstViewDWGInfo.IsEmpty<ViewImpDWGInfo>())
						{
							throw new Exception("拆图失败。");
						}
						this.m_SaveFilePath = this.m_strAimSaveFilePath + "\\All.dwg";
						this.BindingDWGAndUpdateDataShow(this.m_lstViewDWGInfo);
						DWG2RevitLevelConfigForm.m_lstView = this.GetDocSortValidViews();
						this.BindingViewInfosAndUpdateDataShow(DWG2RevitLevelConfigForm.m_lstView);
						this.CreateViews(this.m_SaveFilePath, this.tableLayoutPanel2, ref this.m_viewAllDrawing);
						this.SelectCurViewPreviewObjs(this.m_lstViewDWGInfo[0].m_lstCurve, this.m_viewAllDrawing);
						this.InitBlockFrame(this.m_viewAllDrawing);
						this.CreateViews(this.m_lstViewDWGInfo[0].m_strDWGPath, this.tableLayoutPanel1, ref this.m_viewSingleDrawing);
						this.InitBlockFrame(this.m_viewSingleDrawing);
						DWG2RevitLevelConfigForm.m_b = true;
					}
				}
			}
		}

		private void DWG2RevitLevelConfigForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			if (this.m_ctrlManager != null)
			{
				this.m_ctrlManager.SetControlInfoToForm();
			}
		}

		private void CreateViews(string OdDbDataBasePath, TableLayoutPanel tableLayoutPanel, ref DwgPreview view)
		{
			view.DisposeAll();
			view._hDevice = null;
			view.SetFilePath(OdDbDataBasePath);
			view.RefreshView();
			view.Dock = DockStyle.Fill;
			view.Margin = new Padding(0);
			if (!DWG2RevitLevelConfigForm.m_b)
			{
				tableLayoutPanel.Controls.Add(view);
			}
			tableLayoutPanel.Refresh();
		}

		public void InitBlockFrame(DwgPreview view)
		{
			using (view._hDevice.viewAt(0))
			{
				view.Invalidate();
			}
		}

		public List<ViewBindingDWG> GetDocSortValidViews()
		{
			List<ViewBindingDWG> list = new List<ViewBindingDWG>();
			List<Autodesk.Revit.DB.View> list2 = new List<Autodesk.Revit.DB.View>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.View));
			foreach (Element element in filteredElementCollector)
			{
				Autodesk.Revit.DB.View view = (Autodesk.Revit.DB.View)element;
				if (view.GenLevel != null && (int)view.ViewType == 1)
				{
					double elevation = view.GenLevel.Elevation;
					if (elevation >= -1.7976931348623157E+308 && elevation <= 1.7976931348623157E+308)
					{
						list2.Add(view);
					}
				}
			}
			this.RemoveOutputDrawingView(list2);
			list2.Sort(new DWG2RevitLevelConfigForm.ViewElevationComparer());
			for (int i = 0; i < list2.Count; i++)
			{
				list.Add(new ViewBindingDWG
				{
					m_view = list2[i]
				});
			}
			return list;
		}

		private void RemoveOutputDrawingView(List<Autodesk.Revit.DB.View> lstView)
		{
			try
			{
				for (int i = 0; i < lstView.Count; i++)
				{
					Parameter parameter = lstView[i].GetParameter("视图分类-子");
					if (parameter != null && parameter.AsString().CompareTo("02出图") == 0)
					{
						lstView.RemoveAt(i);
						i--;
					}
				}
			}
			catch (Exception)
			{
				Trace.Assert(true, "楼层过滤异常。");
			}
		}

		private static string GetViewGroupNameFullString(Autodesk.Revit.DB.View view)
		{
			List<string> list = new List<string>();
			Parameter parameter = view.GetParameter("视图分类-父");
			if (parameter != null)
			{
				list.Add(parameter.AsString());
			}
			Parameter parameter2 = view.GetParameter("视图分类-子");
			if (parameter2 != null)
			{
				list.Add(parameter2.AsString());
			}
			return StringUtilityMethod.MergeListString(list);
		}

		public bool SelectCurViewPreviewObjs(List<OdDbCurve> lstOdDbCurve, DwgPreview view)
		{
			if (lstOdDbCurve == null)
			{
				return false;
			}
			if (this.m_lstOdGiDrawable.Count != 0)
			{
				foreach (OdDbCurve sceneGraph in this.m_lstOdGiDrawable)
				{
					this.m_gsView.erase(sceneGraph);
				}
				this.m_lstOdGiDrawable.Clear();
			}
			this.m_gsModelBlockFrame = view._hDevice.createModel();
			this.m_gsView = view._hDevice.viewAt(0);
			OdCmEntityColor odCmEntityColor = new OdCmEntityColor(byte.MaxValue, 0, 0);
			LineWeight lineWeight = LineWeight.kLnWt030;
			foreach (OdDbCurve odDbCurve in lstOdDbCurve)
			{
				this.m_cvClone = OdDbCurve.cast(odDbCurve.clone());
				this.m_cvClone.setColorIndex((ushort)odCmEntityColor.colorIndex());
				this.m_cvClone.setLineWeight(lineWeight);
				this.m_gsView.add(this.m_cvClone, this.m_gsModelBlockFrame);
				this.m_lstOdGiDrawable.Add(this.m_cvClone);
			}
			view.Invalidate();
			return true;
		}

		private void BindingDWGAndUpdateDataShow(List<ViewImpDWGInfo> lstViewDWGInfo)
		{
			this.lvwDrawingList.Items.Clear();
			for (int i = 1; i <= lstViewDWGInfo.Count; i++)
			{
				ListViewItem listViewItem = new ListViewItem(i.ToString(), i);
				listViewItem.SubItems.Add(lstViewDWGInfo[i - 1].m_strDWGName);
				this.lvwDrawingList.Items.Add(listViewItem);
			}
			this.RestDrawingListViewColumnWidth();
		}

		private void RestDrawingListViewColumnWidth()
		{
			this.lvwDrawingList.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
			if (this.lvwDrawingList.Columns[1].Width < this.m_nDrawingListViewColumnWidthMin)
			{
				this.lvwDrawingList.Columns[1].Width = this.m_nDrawingListViewColumnWidthMin;
			}
		}

		public void BindingViewInfosAndUpdateDataShow(List<ViewBindingDWG> lstView)
		{
			this.lvwFloorAndDrawing.Items.Clear();
			for (int i = 1; i <= lstView.Count; i++)
			{
				ListViewItem listViewItem = new ListViewItem(i.ToString(), i);
				listViewItem.SubItems.Add(lstView[i - 1].m_view.Name);
				listViewItem.SubItems.Add("");
				this.lvwFloorAndDrawing.Items.Add(listViewItem);
			}
		}

		public void BindingViewInfosAndUpdateDataShow1(List<ViewBindingDWG> lstView)
		{
			this.lvwFloorAndDrawing.Items.Clear();
			for (int i = 1; i <= lstView.Count; i++)
			{
				ListViewItem listViewItem = new ListViewItem(i.ToString(), i);
				listViewItem.SubItems.Add(lstView[i - 1].m_view.Name);
				listViewItem.SubItems.Add("");
				for (int j = 0; j < DWG2RevitLevelConfigForm.m_lstView.Count; j++)
				{
					try
					{
						if (lstView[i - 1].m_view.Id == DWG2RevitLevelConfigForm.m_lstView[j].m_view.Id && DWG2RevitLevelConfigForm.m_lstView[j].m_viewImpDWGInfo != null && (DWG2RevitLevelConfigForm.m_lstView[j].m_viewImpDWGInfo.m_strDWGPath != null || DWG2RevitLevelConfigForm.m_lstView[j].m_viewImpDWGInfo.m_strDWGPath != ""))
						{
							string strDWGName = DWG2RevitLevelConfigForm.m_lstView[j].m_viewImpDWGInfo.m_strDWGName;
							listViewItem.SubItems[2].Text = strDWGName;
							foreach (char c in strDWGName)
							{
								if (char.IsDigit(c))
								{
									lstView[i - 1].m_viewImpDWGInfo = this.m_lstViewDWGInfo[(int)(c - '1')];
								}
							}
							break;
						}
					}
					catch
					{
					}
				}
				this.lvwFloorAndDrawing.Items.Add(listViewItem);
			}
			DWG2RevitLevelConfigForm.m_lstView = new List<ViewBindingDWG>();
			for (int l = 0; l < lstView.Count; l++)
			{
				DWG2RevitLevelConfigForm.m_lstView.Add(lstView[l]);
			}
		}

		public void DisposeMember()
		{
			if (this.m_gsModelBlockFrame != null)
			{
				this.m_gsModelBlockFrame.Dispose();
				this.m_gsModelBlockFrame = null;
			}
			if (this.m_cvClone != null)
			{
				this.m_cvClone.Dispose();
				this.m_cvClone = null;
			}
		}

		private string SplitString(string str)
		{
			string[] array = str.Split(new char[]
			{
				'\\'
			});
			string[] array2 = array[array.Length - 1].Split(new char[]
			{
				'.'
			});
			return array2[array2.Length - 2];
		}

		private void lvwDrawingList_ItemDrag(object sender, ItemDragEventArgs e)
		{
			IEnumerator enumerator = this.lvwDrawingList.SelectedItems.GetEnumerator();
			{
				if (enumerator.MoveNext())
				{
					DWG2RevitLevelConfigForm.DragDropItemData data = new DWG2RevitLevelConfigForm.DragDropItemData((ListViewItem)enumerator.Current);
					this.lvwDrawingList.DoDragDrop(data, DragDropEffects.Move);
				}
			}
		}

		private void lvwFloorAndDrawing_DragOver(object sender, DragEventArgs e)
		{
			string format = typeof(DWG2RevitLevelConfigForm.DragDropItemData).ToString();
			if (e.Data.GetDataPresent(format))
			{
				if (!(e.Data.GetData(format) is DWG2RevitLevelConfigForm.DragDropItemData))
				{
					return;
				}
				System.Drawing.Point point = this.lvwFloorAndDrawing.PointToClient(new System.Drawing.Point(e.X, e.Y));
				ListViewItem itemAt = this.lvwFloorAndDrawing.GetItemAt(point.X, point.Y);
				if (itemAt != null)
				{
					e.Effect = DragDropEffects.Move;
					if (this.m_lvItemLast == null || (this.m_lvItemLast != null && this.m_lvItemLast != itemAt))
					{
						this.lvwFloorAndDrawing.Invalidate();
					}
					this.lvwFloorAndDrawing.CreateGraphics().DrawRectangle(new Pen(System.Drawing.Color.Red, 2f), itemAt.Bounds);
					this.m_lvItemLast = itemAt;
				}
			}
		}

		private void lvwFloorAndDrawing_DragDrop(object sender, DragEventArgs e)
		{
			string format = typeof(DWG2RevitLevelConfigForm.DragDropItemData).ToString();
			DWG2RevitLevelConfigForm.DragDropItemData dragDropItemData = (DWG2RevitLevelConfigForm.DragDropItemData)e.Data.GetData(format);
			if (dragDropItemData == null)
			{
				return;
			}
			System.Drawing.Point point = this.lvwFloorAndDrawing.PointToClient(new System.Drawing.Point(e.X, e.Y));
			ListViewItem itemAt = this.lvwFloorAndDrawing.GetItemAt(point.X, point.Y);
			if (itemAt != null)
			{
				string text = dragDropItemData.Item.SubItems[1].Text;
				itemAt.SubItems[2].Text = text;
				int num = Convert.ToInt32(itemAt.Text);
				int num2 = 0;
				if (int.TryParse(dragDropItemData.Item.SubItems[0].Text, out num2))
				{
					DWG2RevitLevelConfigForm.m_lstView[num - 1].m_viewImpDWGInfo = this.m_lstViewDWGInfo[num2 - 1];
				}
			}
			this.lvwFloorAndDrawing.Invalidate();
		}

		private void lvwFloorAndDrawing_DragLeave(object sender, EventArgs e)
		{
			this.lvwFloorAndDrawing.Invalidate();
		}

		private void lvwDrawingList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
		{
			if (e.IsSelected)
			{
				int itemIndex = e.ItemIndex;
				this.CreateViews(this.m_lstViewDWGInfo[itemIndex].m_strDWGPath, this.tableLayoutPanel1, ref this.m_viewSingleDrawing);
				this.InitBlockFrame(this.m_viewSingleDrawing);
				this.SelectCurViewPreviewObjs(this.m_lstViewDWGInfo[itemIndex].m_lstCurve, this.m_viewAllDrawing);
				this.InitBlockFrame(this.m_viewAllDrawing);
			}
		}

		private void lvwFloorAndDrawing_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
		{
			if (e.IsSelected)
			{
				int itemIndex = e.ItemIndex;
				string text = this.lvwFloorAndDrawing.SelectedItems[0].SubItems[2].ToString();
				if (text == "" || text == null || DWG2RevitLevelConfigForm.m_lstView == null || DWG2RevitLevelConfigForm.m_lstView[itemIndex] == null || DWG2RevitLevelConfigForm.m_lstView[itemIndex].m_viewImpDWGInfo == null)
				{
					return;
				}
				this.CreateViews(DWG2RevitLevelConfigForm.m_lstView[itemIndex].m_viewImpDWGInfo.m_strDWGPath, this.tableLayoutPanel1, ref this.m_viewSingleDrawing);
				this.InitBlockFrame(this.m_viewSingleDrawing);
				this.SelectCurViewPreviewObjs(DWG2RevitLevelConfigForm.m_lstView[itemIndex].m_viewImpDWGInfo.m_lstCurve, this.m_viewAllDrawing);
				this.InitBlockFrame(this.m_viewAllDrawing);
			}
		}

		private void btnDeleteDrawing_Click(object sender, EventArgs e)
		{
			try
			{
				ListViewItem listViewItem = this.lvwFloorAndDrawing.SelectedItems[0];
				int index = listViewItem.Index;
				listViewItem.SubItems[2].Text = "";
				DWG2RevitLevelConfigForm.m_lstView[index].m_viewImpDWGInfo = null;
			}
			catch
			{
			}
		}

		private void btnFloorSetting_Click(object sender, EventArgs e)
		{
			new DlgLevelSet(this.m_app, this).ShowDialog();
		}

		private void btnDeleteAllDrawing_Click(object sender, EventArgs e)
		{
			DWG2RevitLevelConfigForm.m_lstView = this.GetDocSortValidViews();
			this.BindingViewInfosAndUpdateDataShow(DWG2RevitLevelConfigForm.m_lstView);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			bool flag = false;
			if (!this.SaveSubDrawings(out flag))
			{
				return;
			}
			YJKWindowHandle owner = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			ProgressBarForm progressBarForm = new ProgressBarForm(DWG2RevitLevelConfigForm.m_lstView.Count + 2, "正向楼层中载入图纸……", ProgressBarStyle.Blocks, true, false, false);
			progressBarForm.Show(owner);
			List<string> list = new List<string>();
			this.GetImpCategoryName(ref list);
			for (int i = 0; i < DWG2RevitLevelConfigForm.m_lstView.Count; i++)
			{
				if (progressBarForm.IsDisposed || progressBarForm.m_stopTag)
				{
					return;
				}
				progressBarForm.RefreshProgressBar();
				ElementId elementId = null;
				new DWGImportOptions();
				if (DWG2RevitLevelConfigForm.m_lstView[i].m_viewImpDWGInfo != null)
				{
					using (Transaction transaction = new Transaction(this.m_doc, "LinkDWG"))
					{
						try
						{
							transaction.Start();
							string name = DWG2RevitLevelConfigForm.m_lstView[i].m_view.Name;
							string strDWGPath = DWG2RevitLevelConfigForm.m_lstView[i].m_viewImpDWGInfo.m_strDWGPath;
							this.m_doc.Link(strDWGPath, this.SetDWGImportOptions(), DWG2RevitLevelConfigForm.m_lstView[i].m_view, ref elementId);
							OdGePoint3d odGePoint3d = DWG2RevitLevelConfigForm.m_lstView[i].m_viewImpDWGInfo.m_ArPoint3d[0];
							XYZ refernecePt = new XYZ(odGePoint3d.x, odGePoint3d.y, odGePoint3d.z);
							Element element = this.m_doc.GetElement(elementId);
							element.Pinned = false;
							this.MoveDWGDesignatedSpot(refernecePt, element.Id);
							transaction.Commit();
						}
						catch
						{
							transaction.RollBack();
						}
					}
				}
			}
			if (progressBarForm.IsDisposed || progressBarForm.m_stopTag)
			{
				progressBarForm.RefreshProgressBar();
			}
			if (flag)
			{
				try
				{
					List<string> list2 = new List<string>();
					foreach (ViewImpDWGInfo viewImpDWGInfo in this.m_lstViewDWGInfo)
					{
						string text = this.ReplaceSpcialCharByUnderline(viewImpDWGInfo.m_strDWGName);
						string text2 = text;
						int num = 0;
						while (list2.Contains(text2))
						{
							num++;
							text2 = text + num.ToString();
						}
						list2.Add(text2);
						new FileInfo(viewImpDWGInfo.m_strDWGPath);
						string destFileName = this.m_strAimSaveFilePath + "\\" + text2 + ".dwg";
						File.Move(viewImpDWGInfo.m_strDWGPath, destFileName);
					}
					goto IL_271;
				}
				catch (Exception)
				{
					YJKMessageBox.Show("保存拆分的图纸失败！");
					goto IL_271;
				}
			}
			this.DeleteTmpFiles();
			IL_271:
			if (progressBarForm.IsDisposed || progressBarForm.m_stopTag)
			{
				progressBarForm.RefreshProgressBar();
			}
			if (!progressBarForm.IsDisposed)
			{
				progressBarForm.Dispose();
			}
			base.Close();
		}

		private bool RenameDrawingFile(string strFileFullPath, ref List<string> lstImpCategoryName, ref string strNewFileFullPath)
		{
			try
			{
				int num = strFileFullPath.LastIndexOf('\\');
				string str = strFileFullPath.Remove(num + 1);
				string text = strFileFullPath.Substring(num + 1);
				int startIndex = text.LastIndexOf('.');
				text = text.Remove(startIndex);
				strNewFileFullPath = strFileFullPath;
				string text2 = text;
				int num2 = 0;
				while (lstImpCategoryName.Contains(text2) || File.Exists(strNewFileFullPath))
				{
					string str2 = text;
					string str3 = "_";
					int num3 = num2;
					num2 = num3 + 1;
					text2 = str2 + str3 + num3.ToString();
					strNewFileFullPath = str + text2 + ".dwg";
				}
				lstImpCategoryName.Add(text2);
				if (text2 != text)
				{
					strNewFileFullPath = str + text2 + ".dwg";
					File.Move(strFileFullPath, strNewFileFullPath);
					return true;
				}
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
			}
			return false;
		}

		private void GetImpCategoryName(ref List<string> lstImpCategoryName)
		{
			try
			{
				foreach (Element element in new FilteredElementCollector(this.m_doc).OfClass(typeof(ImportInstance)).ToElements())
				{
					if (!lstImpCategoryName.Contains(element.Category.Name))
					{
						lstImpCategoryName.Add(element.Category.Name);
					}
				}
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
			}
		}

		private bool SaveSubDrawings(out bool bSaveDefault)
		{
			bSaveDefault = false;
			string selectedPath = string.Empty;
			try
			{
				selectedPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			}
			catch (Exception)
			{
				selectedPath = Path.GetPathRoot(this.m_strAimSaveFilePath);
			}
			this.fbdDrawingSavePath.SelectedPath = selectedPath;
			if (this.fbdDrawingSavePath.ShowDialog() == DialogResult.OK)
			{
				try
				{
					string tmpDrawingDirName = this.GetTmpDrawingDirName();
					string text = this.fbdDrawingSavePath.SelectedPath + "\\" + tmpDrawingDirName + "\\";
					if (!text.Contains(this.m_strAimSaveFilePath))
					{
						if (Directory.Exists(text))
						{
							Directory.Delete(text, true);
						}
						Directory.CreateDirectory(text);
						List<string> list = new List<string>();
						using (List<ViewImpDWGInfo>.Enumerator enumerator = this.m_lstViewDWGInfo.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								ViewImpDWGInfo viewImpDWGInfo = enumerator.Current;
								string text2 = this.ReplaceSpcialCharByUnderline(viewImpDWGInfo.m_strDWGName);
								string text3 = text2;
								int num = 0;
								while (list.Contains(text3))
								{
									num++;
									text3 = text2 + num.ToString();
								}
								list.Add(text3);
								string text4 = text + "\\" + text3 + ".dwg";
								File.Copy(viewImpDWGInfo.m_strDWGPath, text4, true);
								viewImpDWGInfo.m_strDWGPath = text4;
								viewImpDWGInfo.m_strDWGName = text3;
							}
							goto IL_13A;
						}
					}
					bSaveDefault = true;
					IL_13A:
					return true;
				}
				catch (Exception)
				{
					YJKMessageBox.Show("保存拆分的图纸失败！");
					return false;
				}
				return false;
			}
			return false;
		}

		private string GetTmpDrawingDirName()
		{
			string text = this.m_strAimSaveFilePath;
			if (text.Last<char>() == '\\')
			{
				text = text.Remove(this.m_strAimSaveFilePath.Count<char>() - 1);
			}
			int num = text.LastIndexOf('\\');
			return text.Substring(num + 1);
		}

		private string ReplaceSpcialCharByUnderline(string strFileName)
		{
			StringBuilder stringBuilder = new StringBuilder(strFileName);
			foreach (char c in Path.GetInvalidFileNameChars())
			{
				stringBuilder.Replace(c.ToString(), "_");
			}
			return stringBuilder.ToString();
		}

		internal bool GetDrawNum(string strDrawName, out int nDrawNum)
		{
			nDrawNum = 0;
			StringBuilder stringBuilder = new StringBuilder();
			bool result;
			try
			{
				foreach (char c in strDrawName.ToCharArray())
				{
					if (this.IsNumberic(c))
					{
						stringBuilder.Append(c);
					}
				}
				result = int.TryParse(stringBuilder.ToString(), out nDrawNum);
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool IsNumberic(char chText)
		{
			bool result;
			try
			{
				if (chText < '0' || chText > '9')
				{
					result = false;
				}
				else
				{
					result = true;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DeleteTmpFiles();
			base.Close();
		}

		private void DeleteTmpFiles()
		{
			try
			{
				if (Directory.Exists(this.m_strAimSaveFilePath))
				{
					Directory.Delete(this.m_strAimSaveFilePath, true);
				}
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
			}
		}

		protected override void OnClosed(EventArgs e)
		{
			this.m_viewSingleDrawing.DisposeAll();
			this.m_viewAllDrawing.DisposeAll();
			base.OnClosed(e);
		}

		private DWGImportOptions SetDWGImportOptions()
		{
			return new DWGImportOptions
			{
				AutoCorrectAlmostVHLines = true,
				ColorMode = 0,
				Placement = 1,
				OrientToView = true,
				ThisViewOnly = true,
				Unit = 6,
				VisibleLayersOnly = true
			};
		}

		private void MoveDWGDesignatedSpot(XYZ refernecePt, ElementId elemDWGId)
		{
			XYZ xyz = new XYZ(Common.MMToFeet(refernecePt.X), Common.MMToFeet(refernecePt.Y), Common.MMToFeet(refernecePt.Z));
			XYZ xyz2 = XYZ.Zero - xyz;
			ElementTransformUtils.MoveElement(this.m_doc, elemDWGId, xyz2);
		}

		private void DWG2RevitLevelConfigForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void lvwDrawingList_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.m_lviDoubleSelect = this.lvwDrawingList.GetItemAt(this.m_pntMousePos.X, this.m_pntMousePos.Y);
			System.Drawing.Rectangle bounds = this.m_lviDoubleSelect.GetBounds(ItemBoundsPortion.Entire);
			int num = bounds.Left;
			int num2 = 0;
			int scrollPos = DWG2RevitLevelConfigForm.GetScrollPos(this.lvwDrawingList.Handle.ToInt32(), 0);
			foreach (object obj in this.lvwDrawingList.Columns)
			{
				ColumnHeader columnHeader = (ColumnHeader)obj;
				if (this.m_pntMousePos.X + scrollPos >= num + columnHeader.Width)
				{
					num += columnHeader.Width;
					num2++;
				}
			}
			if (this.m_lviDoubleSelect != null && num2 == 1)
			{
				bounds.X = num;
				bounds.Width = this.lvwDrawingList.Columns[num2].Width;
				this.tbxEditDrawingName.Bounds = bounds;
				this.tbxEditDrawingName.Text = this.m_lviDoubleSelect.SubItems[num2].Text;
				this.tbxEditDrawingName.Visible = true;
				this.tbxEditDrawingName.Focus();
			}
		}

		private void tbxEditDrawingName_Leave(object sender, EventArgs e)
		{
			this.ApplyNewDrawingName();
		}

		[DllImport("user32")]
		private static extern int GetScrollPos(int hwnd, int nBar);

		private void lvwDrawingList_MouseMove(object sender, MouseEventArgs e)
		{
			this.m_pntMousePos.X = e.X;
			this.m_pntMousePos.Y = e.Y;
		}

		private void ApplyNewDrawingName()
		{
			try
			{
				if (StringUtilityMethod.RemoveSpace(this.tbxEditDrawingName.Text).Length < 1)
				{
					YJKMessageBox.Show("请至少输入一个非空字符。");
					this.tbxEditDrawingName.Focus();
					return;
				}
				if (this.m_lviDoubleSelect != null)
				{
					foreach (object obj in this.lvwDrawingList.Items)
					{
						ListViewItem listViewItem = (ListViewItem)obj;
						if (listViewItem.Index != this.m_lviDoubleSelect.Index && listViewItem.SubItems[1].Text.CompareTo(this.tbxEditDrawingName.Text) == 0)
						{
							YJKMessageBox.Show("当前输入的图名已存在，请重新输入。");
							this.tbxEditDrawingName.Focus();
							return;
						}
					}
					this.m_lviDoubleSelect.SubItems[1].Text = this.tbxEditDrawingName.Text;
					this.RestDrawingListViewColumnWidth();
					int num = 0;
					int.TryParse(this.m_lviDoubleSelect.SubItems[0].Text, out num);
					this.m_lstViewDWGInfo[num - 1].m_strDWGName = this.m_lviDoubleSelect.SubItems[1].Text;
					string strDWGPath = this.m_lstViewDWGInfo[num - 1].m_strDWGPath;
					for (int i = 0; i < DWG2RevitLevelConfigForm.m_lstView.Count; i++)
					{
						if (DWG2RevitLevelConfigForm.m_lstView[i] != null && DWG2RevitLevelConfigForm.m_lstView[i].m_viewImpDWGInfo != null && DWG2RevitLevelConfigForm.m_lstView[i].m_viewImpDWGInfo.m_strDWGPath.CompareTo(strDWGPath) == 0)
						{
							DWG2RevitLevelConfigForm.m_lstView[i].m_viewImpDWGInfo.m_strDWGName = this.m_lviDoubleSelect.SubItems[1].Text;
							this.lvwFloorAndDrawing.Items[i].SubItems[2].Text = this.m_lviDoubleSelect.SubItems[1].Text;
						}
					}
					this.m_lviDoubleSelect = null;
				}
			}
			catch (Exception)
			{
				Trace.Assert(true, "图名更新失败！");
			}
			this.tbxEditDrawingName.Visible = false;
		}

		private void tbxEditDrawingName_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Return)
			{
				this.ApplyNewDrawingName();
				e.Handled = true;
				return;
			}
			if (e.KeyCode == Keys.Escape)
			{
				this.tbxEditDrawingName.Visible = false;
				e.Handled = true;
				return;
			}
		}

		private void viewAllDrawing_MouseClick(object sender, MouseEventArgs e)
		{
			if (e.Button != MouseButtons.Left)
			{
				return;
			}
			try
			{
				OdGePoint2d screenPoint = new OdGePoint2d((double)e.X, (double)e.Y);
				OdGePoint3d worldCoordinateByScreen = this.m_viewAllDrawing.getWorldCoordinateByScreen(screenPoint);
				int index = 0;
				bool flag = false;
				for (int i = 0; i < this.m_lstViewDWGInfo.Count; i++)
				{
					OdGePoint3d[] arPoint3d = this.m_lstViewDWGInfo[i].m_ArPoint3d;
					OdGePoint3d startPoint = new OdGePoint3d();
					this.m_lstViewDWGInfo[i].m_lstCurve[0].getStartPoint(startPoint);
					double num = arPoint3d.Min((OdGePoint3d thisPnt) => thisPnt.x);
					double num2 = arPoint3d.Max((OdGePoint3d thisPnt) => thisPnt.x);
					double num3 = arPoint3d.Min((OdGePoint3d thisPnt) => thisPnt.y);
					double num4 = arPoint3d.Max((OdGePoint3d thisPnt) => thisPnt.y);
					if (worldCoordinateByScreen.x > num && worldCoordinateByScreen.x < num2 && worldCoordinateByScreen.y > num3 && worldCoordinateByScreen.y < num4)
					{
						index = i;
						flag = true;
						break;
					}
				}
				if (flag)
				{
					this.lvwDrawingList.Items[index].Selected = true;
					this.lvwDrawingList.Items[index].Checked = true;
				}
			}
			catch (Exception ex)
			{
				Trace.Assert(true, ex.Message);
			}
		}

		private void lvwDrawingList_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
		{
			if (e.ColumnIndex == 0)
			{
				e.Cancel = true;
				e.NewWidth = 0;
			}
		}

		private void DWG2RevitLevelConfigForm_ResizeBegin(object sender, EventArgs e)
		{
			this.tableLayoutPanel1.SuspendLayout();
			this.tableLayoutPanel2.SuspendLayout();
		}

		private void DWG2RevitLevelConfigForm_ResizeEnd(object sender, EventArgs e)
		{
			this.tableLayoutPanel1.ResumeLayout();
			this.tableLayoutPanel2.ResumeLayout();
			this.tableLayoutPanel1.Refresh();
			this.tableLayoutPanel2.Refresh();
		}

		private void DWG2RevitLevelConfigForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_ctrlManager != null)
			{
				this.m_ctrlManager.GetControlInfoToXml();
			}
		}

		private Document m_doc;

		private UIApplication m_app;

		private string m_OdDbDataBasePath;

		private DwgPreview m_viewSingleDrawing = new DwgPreview();

		private DwgPreview m_viewAllDrawing = new DwgPreview();

		private OdGsView m_gsView;

		private ListViewItem m_lvItemLast;

		private OdGsModel m_gsModelBlockFrame;

		private List<ViewImpDWGInfo> m_lstViewDWGInfo;

		private static List<ViewBindingDWG> m_lstView;

		private OdDbCurve m_cvClone;

		private static bool m_b = false;

		private List<OdDbCurve> m_lstOdGiDrawable = new List<OdDbCurve>();

		private System.Drawing.Point m_pntMousePos = new System.Drawing.Point(0, 0);

		private ListViewItem m_lviDoubleSelect;

		private static bool m_bOK = true;

		private string m_SaveFilePath = Product.UserDataLocation + "\\图纸预处理\\All.dwg";

		private int m_nDrawingListViewColumnWidthMin;

		private string m_strAimSaveFilePath;

		private ControlInfoManager m_ctrlManager;

		public class ViewElevationComparer : IComparer<Autodesk.Revit.DB.View>
		{
			public int Compare(Autodesk.Revit.DB.View x, Autodesk.Revit.DB.View y)
			{
				string viewGroupNameFullString = DWG2RevitLevelConfigForm.GetViewGroupNameFullString(x);
				string viewGroupNameFullString2 = DWG2RevitLevelConfigForm.GetViewGroupNameFullString(y);
				int num = viewGroupNameFullString.CompareTo(viewGroupNameFullString2);
				if (num != 0)
				{
					return num;
				}
				return y.GenLevel.Elevation.CompareTo(x.GenLevel.Elevation);
			}
		}

		private class DragDropItemData
		{
			public DragDropItemData(ListViewItem item)
			{
				this.Item = item;
			}

			public ListViewItem Item { get; set; }
		}
	}
}
